﻿#include "singleton.h"


QScopedPointer<Singleton> Singleton::self;
Singleton *Singleton::Instance()
{
    if (self.isNull())
    {
        static QMutex mutex;
        QMutexLocker locker(&mutex);
        if (self.isNull())
        {
            self.reset(new Singleton);
        }
    }

    return self.data();
}
Singleton::Singleton(QObject *parent) : QObject(parent)
{

}

void Singleton::onTextMessageReceived(const QString &message)
{
    qDebug() << "received:" << message;

    QJsonParseError receivedJsonParseError;
    QJsonDocument receivedJsonDoc = QJsonDocument::fromJson(message.toStdString().data(), &receivedJsonParseError);
    if(!receivedJsonDoc.isEmpty() && (receivedJsonParseError.error == QJsonParseError::NoError))
    {
        if(receivedJsonDoc.isObject())
        {
            QJsonObject receivedJsonObj = receivedJsonDoc.object();
            if(receivedJsonObj.contains("type"))
            {
                QString type = receivedJsonObj.value("type").toString();
                if(type == "response")
                {
                    if(receivedJsonObj.contains("method"))
                    {
                        QString method = receivedJsonObj.value("method").toString();

                        if(receivedJsonObj.contains("body"))
                        {
                            QJsonObject bodyJsonObj = receivedJsonObj.value("body").toObject();
                            if(method == "GetDeviceInfo")
                            {
                                emit sigGetDeviceInfo(bodyJsonObj);
                            }
                            else if(method == "GetDeviceRunInfo")
                            {
                                emit sigGetDeviceRunInfo(bodyJsonObj);
                            }
                            else if(method == "GetNetworkParam")
                            {
                                emit sigGetNetworkParam(bodyJsonObj);
                            }
                            else if(method == "GetAlgoParam")
                            {
                                emit sigGetAlgoParam(bodyJsonObj);
                            }
                            else if(method == "GetZoneParam")
                            {
                                emit sigGetZoneParam(bodyJsonObj);
                            }
                            else if(method == "GetAlgoInfo")
                            {
                                emit sigGetAlgoInfo(bodyJsonObj);
                            }

                            else if(method == "GetExternalParam")
                            {
                                emit sigGetExternalParam(bodyJsonObj);
                            }
                            else if(method == "GetAlarmOut")
                            {
                                emit sigGetAlarmOut(bodyJsonObj);
                            }
                            else if(method == "GetAlgoPeopleCountParam")
                            {
                                emit sigGetAlgoPeopleCountParam(bodyJsonObj);
                            }
                            else if(method == "GetCoalSpdParam")
                            {
                                emit sigGetCoalSpdParam(bodyJsonObj);
                            }
                            else if(method == "GetCameraParam")
                            {
                                emit sigGetCameraParam(bodyJsonObj);
                            }
                            else if(method == "GetBeltTearParam")
                            {
                                emit sigGetBeltTearParam(bodyJsonObj);
                            }
                            else if(method == "GetPileCoalParam")
                            {
                                emit sigGetPileCoalParam(bodyJsonObj);
                            }
                            else if(method == "GetColidarPileCoalParam")
                            {
                                emit sigGetColidarPileCoalParam(bodyJsonObj);
                            }
                            else if(method == "GetAlgoPeopleCountMaxByTime")
                            {
                                emit sigGetAlgoPeopleCountMaxByTime(bodyJsonObj);
                            }
                            else if(method == "GetbeltWidth")
                            {
                                emit sigGetbeltWidth(bodyJsonObj);
                            }
                            else if(method == "GetCorrectZone")
                            {
                                emit sigGetCorrectZone(bodyJsonObj);
                            }
                        }
                        //状态
                        if(method == "SetNetworkParam")
                        {
                            emit sigSetNetworkParamSuccess();
                        }
                        else if(method == "SyncDateTime")
                        {
                            emit sigSyncDateTimeSuccess();
                        }
                        else if(method == "SetAlarmInfo")
                        {
                            emit sigSetAlarmInfoSuccess();
                        }
                        else if(method == "SetColidarPileCoalParam")
                        {
                            emit sigSetColidarPileCoalParamSuccess();
                        }
                        else if(method == "SetCoalSpdParam")
                        {
                            emit sigSetCoalSpdParamSuccess();
                        }
                        else if(method == "SetCoalSpdBaseline")
                        {
                            emit sigSetCoalSpdBaselineSuccess();
                        }

                    }
//                    else if(receivedJsonObj.contains("method"))
//                    {
//                        QString method = receivedJsonObj.value("method").toString();

//                        if(method == "SetNetworkParam")
//                        {
//                            emit sigSetNetworkParamSuccess();
//                        }
//                        else if(method == "SyncDateTime")
//                        {
//                            emit sigSyncDateTimeSuccess();
//                        }
//                        else if(method == "SetAlarmInfo")
//                        {
//                            emit sigSetAlarmInfoSuccess();
//                        }
//                        else if(method == "SetColidarPileCoalParam")
//                        {
//                            emit sigSetColidarPileCoalParamSuccess();
//                        }
//                        else if(method == "SetCoalSpdParam")
//                        {
//                            emit sigSetCoalSpdParamSuccess();
//                        }
//                        else if(method == "SetCoalSpdBaseline")
//                        {
//                            emit sigSetCoalSpdBaselineSuccess();
//                        }
//                    }
                }
                else if(type == "notice")
                {
                    if(receivedJsonObj.contains("method") && receivedJsonObj.contains("body"))
                    {
                        QString method = receivedJsonObj.value("method").toString();
                        QJsonObject bodyJsonObj = receivedJsonObj.value("body").toObject();

                        if(method == "AppUpgrade")
                        {
                            //handleAppUpgrade(bodyJsonObj);
                            emit sigAppUpgrade(bodyJsonObj);

                        }
                        else if(method == "CoalSpdValue")
                        {
                            //handleCoalSpdValue(bodyJsonObj);
                            emit sigCoalSpdValue(bodyJsonObj);
                        }
                        else if(method == "AlgoPeopleCountMaxChanged")
                        {
                            emit sigPeopleCountMaxChanged(bodyJsonObj);
                        }
                    }
                }
                else if(type == "request")
                {
                }
            }
        }
    }
}

void  Singleton::onBinaryMessageReceived(const QByteArray &message)
{
    // header

    WsBinHeader header;
    int headerLen = sizeof(WsBinHeader);
    memcpy(&header, message.data(), headerLen);
//    if(header.chn != m_chn)
//    {
//        return;
//    }
    // 接收图片 message = header + image + alarm_extern
    if(header.tag == WSBIN_TYPE_IMAGE && message.length() == (headerLen + header.totalSize))
    {
        // image
        QByteArray imageArr;
        // alarm_extern 为0xa5时，包含拓展报警信息
        int alarmExternLen = 8 * 1024;

        // 报警图片获取
        int imageLen = message.length() - headerLen - alarmExternLen;
        imageArr.resize(imageLen);
        memcpy(imageArr.data(), message.data() + headerLen, imageLen);

        // 拓展信息
        QByteArray alarmExternArr;
        alarmExternArr.resize(alarmExternLen);
        memcpy(alarmExternArr.data(), message.data() + headerLen + imageLen, alarmExternLen);
        QJsonParseError jsonParseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(alarmExternArr.data(), &jsonParseError);

        if(!jsonDoc.isEmpty() && (jsonParseError.error == QJsonParseError::NoError))
        {
            if(jsonDoc.isObject())
            {
                AlarmExtern alarmExtern = handleAlarmExtern(jsonDoc.object());
                emit sigAlarmImage(header, imageArr, alarmExtern);
            }
        }
    }

    // 接收语音文件 message = header + voice
    if(header.tag == WSBIN_TYPE_VOICE && message.length() == (headerLen + header.totalSize))
    {
        // voice
        QByteArray voiceArr;

        // 语音文件获取
        int voiceLen = message.length() - headerLen;
        voiceArr.resize(voiceLen);
        memcpy(voiceArr.data(), message.data() + headerLen, voiceLen);

        emit sigVoiceTalkOut(header, voiceArr);
    }
}

void Singleton:: onSendParam(const QString &method, QJsonObject body)
{
    emit sigsendRequest(method, 0, body);
}

void  Singleton::sendBinaryMessage(const QByteArray &message)
{
    emit sendBinaryMessage(message);
}

AlarmExtern Singleton::handleAlarmExtern(QJsonObject obj)
{
    AlarmExtern alarmExtern;
    alarmExtern.chnId = obj.value("chnId").toString();
    alarmExtern.startTime = obj.value("start_time").toString();
    alarmExtern.stopTime = obj.value("stop_time").toString();
    alarmExtern.alarmType = obj.value("alarmType").toInt();
    alarmExtern.alarmImageId = obj.value("alarmImageId").toString();
    alarmExtern.desc = obj.value("desc").toString();
    alarmExtern.alarmGrade = obj.value("alarmGrade").toInt();

    QJsonArray jsonArr = obj.value("boxes").toArray();
    QJsonDocument jsonDoc = QJsonDocument(jsonArr);
    alarmExtern.boxes = QString(jsonDoc.toJson());

    alarmExtern.steps = obj.value("steps").toInt();

    jsonArr = obj.value("zones").toArray();
    jsonDoc = QJsonDocument(jsonArr);
    alarmExtern.zones = QString(jsonDoc.toJson());

    jsonArr = obj.value("lines").toArray();
    jsonDoc = QJsonDocument(jsonArr);
    alarmExtern.lines = QString(jsonDoc.toJson());
    return alarmExtern;
}
